package homework01;

import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.Map.Entry;

public class StudentItemController {
    static StudentItemService ss = new StudentItemService();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(true) {
            showMenu();
            int flag = scanner.nextInt();
            if(flag == 1) {
                showAll();
            }else if(flag == 2) {
                //
                System.out.println("请输入学生的ID：");
                int id = scanner.nextInt();
                System.out.println("请输入学生的的姓名：");
                String name = scanner.next();
                System.out.println("请输入学生的年龄：");
                int age = scanner.nextInt();
                //封装视图层的数据
                StudentItem si = new StudentItem();
                si.setId(id);
                si.setName(name);
                si.setAge(age);
                //
                int flags = ss.addStudentItem(si);
                if(flags == 100) {
                    System.out.println("添加成功！");
                }else{
                    System.out.println("操作失败！");
                }
            }else if(flag == 3) {
                System.out.println("请输入要删除学生的ID：");
                int id = scanner.nextInt();
                int delNum = ss.deleteStudentItem(id);
                if(delNum == 1) {
                    System.out.println("删除成功！");
                }else if(delNum == -100) {
                    System.out.println("学生不存在！");
                }else {
                    System.out.println("删除失败！");
                }
                //showAll();
            }else if(flag == 4) {
                System.out.println("系统退出！谢谢使用！");
                System.exit(0);
            }
        }

    }

    private static void showMenu() {
        System.out.println("------ 学生信息模块 --------------");
        System.out.println("1、显示所有学生的信息");
        System.out.println("2、添加学生");
        System.out.println("3、删除学生");
        System.out.println("4、退出系统");
        System.out.println("请输入你要操作的选择：");
    }

    private static void showAll() {
        Map<Integer, StudentItem> map = ss.findAll();// static StudentItemService ss = new StudentItemService();
        System.out.println("学生学号 "+"\t"+"学生姓名 "+"\t"+"学生年龄 ");
        Set<Entry<Integer, StudentItem>> set = map.entrySet();
        Iterator<Entry<Integer, StudentItem>>  it = set.iterator();
        while(it.hasNext()) {
            Entry<Integer, StudentItem> entry = it.next();
            StudentItem si = entry.getValue();
            System.out.println(si.getId()+"     \t\t"+si.getName()+"    \t"+si.getAge());

        }

    }

}
